package cn.edu.csust.tysp.backend.controller;

import cn.edu.csust.tysp.backend.common.ApiPageResult;
import cn.edu.csust.tysp.backend.common.ApiResult;
import cn.edu.csust.tysp.backend.domain.Sheet;
import cn.edu.csust.tysp.backend.service.SheetService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @ClassName: MusicSheetController
 * @Author: XijiaoZheng
 * @E-mail: 2516299639@qq.com
 * @Date: 2021/5/9 15:14
 * @Version: V1.0
 * @Description: 对该类的描述
 */

@RestController
public class SheetController {

    @Autowired
    SheetService sheetService;
    /*
     *查找乐谱
     * @return
     * @throws
     */
    @GetMapping("/sheetmusic")
    public JSONObject selectSheet(String sheetName,String instrumentType) {
        if(sheetName==null)
            return ApiPageResult.error("628","请求格式错误").toJsonObject();
        PageInfo<Sheet> sheets;
        if(instrumentType==null){
            sheets = sheetService.selectByName(sheetName);
        }else{
            sheets = sheetService.selectSheet(sheetName,instrumentType);
        }
        ApiResult<PageInfo<Sheet>> apiResult=new ApiResult<>();
        apiResult.setSuccess(true);
        apiResult.setData(sheets);
        return apiResult.toJsonObject();
    }


    @GetMapping("/tuning")
    public JSONObject play(@RequestParam("tunefile") MultipartFile file) {

        String fileName;
        String suffixName;
        if (!file.isEmpty()) {
            // 获取文件名
            fileName = file.getOriginalFilename();
            // 获取文件的后缀名
            suffixName = fileName.substring(fileName.lastIndexOf("."));
            // 文件上传后的路径
            String filePath = "D:\\郑喜姣学习\\听音识谱项目\\文档";
            File dest = new File(filePath + fileName);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }try {
                file.transferTo(dest);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            return ApiPageResult.error("tysp-501","文件为空").toJsonObject();
        }
        //System.out.println("tuneUrl"+tuneURL+"   文件地址");
        //int score=
        int shirt=90;
        ApiResult<Integer> apiResult=new ApiResult<>();
        apiResult.setData(shirt);
        apiResult.isSuccess();
        System.out.println(apiResult);
        return apiResult.toJsonObject();
    }
}
